import { Component, ViewChild, ChangeDetectorRef } from "@angular/core";
import {
  NavController,
  AlertController,
  NavParams,
  Content
} from "ionic-angular";

import { Storage } from "@ionic/storage";
import { HttpService } from "../../../providers/http.service";
import { WorkSumPage } from "../../work-sum/work-sum";
import { CommonFunc } from "../../../providers/commonFunc";
import { MaintUserAddPage } from "../../maint-user-add/maintUserAdd";
@Component({
  templateUrl: "hydpress.html",
  providers: [HttpService, CommonFunc]
})
export class HydpressPage {
  @ViewChild(Content) content: Content;
  private mainceType: any = 1; //初始 半月保
  private getType: number;
  private elevatorInfo: any;
  private param: any;
  isLook: boolean = false;
  constructor(
    public navCtrl: NavController,
    private storage: Storage,
    private httpService: HttpService,
    private alert: AlertController,
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    public cd: ChangeDetectorRef
  ) {
    this.getType = this.navParams.get("type") * 1;
    this.param = this.navParams;
    this.elevatorInfo = this.navParams.get("elevatorInfo");
    this.isLook = this.navParams.get("isLook");
  }
  segmentChanged(event) {
    //  debugger;
    console.log("------");
  }
  doSth() {
    // debugger;
  }
  //切换到下个
  doNext(event) {
    if (this.mainceType < this.getType) {
      this.mainceType++;
      this.content.scrollToTop();
      this.cd.detectChanges();
    } else {
      if (this.isLook) {
        this.navCtrl.push(MaintUserAddPage, {
          elevatorNum: this.param.data.elevatorNum,
          ElevaType: event.ElevaType,
          isLook: this.isLook
        });
        // let toas = this.commonFunc.showInsideErrorToast({
        //   errorMessage: "已看完，请确认签字"
        // });
        // toas.present();
        return;
      }
      let list = ["operatingDevice", "cartopOverhaulDevice", "pitStop"];

      this.storage.get(this.param.data.elevatorNum + "hydpress-half").then(data => {
        // let dd = this.commonFunc.convertBean(data);
        let res1 = this.commonFunc.vlidate(data, list, false);
        if (!res1) {
          let toas = this.commonFunc.showInsideErrorToast({
            errorMessage: "请拍全照片，填写第一步中打x项说明"
          });
          toas.present();
          return;
        }
        if (this.getType > 1) {
          this.storage.get(this.param.data.elevatorNum + "hydpress-quarter").then(data => {
            // let dd = this.commonFunc.convertBean(data);
            let res2 = this.commonFunc.vlidate(data, [], false);
            if (!res2) {
              let toas = this.commonFunc.showInsideErrorToast({
                errorMessage: "请拍全照片，填写第二步中打x项说明"
              });
              toas.present();
              return;
            }
            if (this.getType > 2) {
              this.storage.get(this.param.data.elevatorNum + "hydpress-half-year").then(data => {
                // let dd = this.commonFunc.convertBean(data);
                let res3 = this.commonFunc.vlidate(data, [], false);
                if (!res3) {
                  let toas = this.commonFunc.showInsideErrorToast({
                    errorMessage: "请拍全照片，填写第三步中打x项说明"
                  });
                  toas.present();
                  return;
                }
                if (this.getType > 3) {
                  this.storage.get(this.param.data.elevatorNum + "hydpress-year").then(data => {
                    // let dd = this.commonFunc.convertBean(data);
                    let res4 = this.commonFunc.vlidate(data, [], false);
                    if (!res4) {
                      let toas = this.commonFunc.showInsideErrorToast({
                        errorMessage: "请拍全照片，填写第四步中打x项说明"
                      });
                      toas.present();
                      return;
                    } else {
                      this.cd.detectChanges();
                      this.navCtrl.push(MaintUserAddPage, {
                        elevatorNum: this.param.data.elevatorNum,
                        ElevaType: event.ElevaType,
                        elevatorInfo: this.elevatorInfo,
                        date: this.navParams.get("date"),
                        xcfhcs: this.navParams.get("xcfhcs"),
                        ldbh: this.navParams.get("ldbh"),
                      });
                    }
                  });
                } else {
                  this.cd.detectChanges();
                  this.navCtrl.push(MaintUserAddPage, {
                    elevatorNum: this.param.data.elevatorNum,
                    ElevaType: event.ElevaType,
                    elevatorInfo: this.elevatorInfo,
                    date: this.navParams.get("date"),
                    xcfhcs: this.navParams.get("xcfhcs"),
                    ldbh: this.navParams.get("ldbh"),
                  });
                }
              });
            } else {
              this.cd.detectChanges();
              this.navCtrl.push(MaintUserAddPage, {
                elevatorNum: this.param.data.elevatorNum,
                ElevaType: event.ElevaType,
                elevatorInfo: this.elevatorInfo,
                date: this.navParams.get("date"),
                xcfhcs: this.navParams.get("xcfhcs"),
                ldbh: this.navParams.get("ldbh"),
              });
            }
          });
        } else {
          this.cd.detectChanges();
          this.navCtrl.push(MaintUserAddPage, {
            elevatorNum: this.param.data.elevatorNum,
            ElevaType: event.ElevaType,
            elevatorInfo: this.elevatorInfo,
            date: this.navParams.get("date"),
            xcfhcs: this.navParams.get("xcfhcs"),
            ldbh: this.navParams.get("ldbh"),
          });
        }
      });
    }
  }

  doBack(event) {
    this.mainceType--;
    this.content.scrollToTop();
    this.cd.detectChanges();
  }
}
